package com.accessibleworlds.blind;

import java.util.ArrayList;

import com.accessibleworlds.R;
import com.accessibleworlds.util.SharedVariables;
import com.accessibleworlds.util.TextToSpeeches;
import com.accessibleworlds.util.TextToSpeechesGoogle;
import com.accessibleworlds.util.TextToSpeechesPico;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;

/**
 * Represents the base class for a blind user activity.
 * @author Ayberk Özgür
 *
 */
public class BlindUserSpeechToTextActivity extends Activity implements MultitouchGestureListener{
	private final int VOICE_RECOGNITION_REQUEST_CODE = 999;

	private TextToSpeeches tts = null;
	private boolean ttsReady = false;

	private String result = null;
	private String itemToGet = null;
	private String language = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.blinduserpage);

		itemToGet = getIntent().getExtras().getString("item_to_get");
		language = getIntent().getExtras().getString("language");

		new Thread(new Runnable() {
			public void run() {
				startTTS();
			}
		}).start();
	}

	@Override
	public void onBackPressed() {
	}

	private void startTTS() {
		if("0".equalsIgnoreCase(getResources().getStringArray(R.array.languageValues)[SharedVariables.currentLanguage]))
			tts = new TextToSpeechesPico();
		else
			tts = new TextToSpeechesGoogle();
		tts.speak(BlindUserSpeechToTextActivity.this, "Please speak your "+itemToGet+" now.");
		ttsReady = true;
		while(tts.isSpeakingNow());
		runOnUiThread(new Runnable() {
			public void run() {
				launchRecognizer();
			}
		});
	}

	private void relaunchTTS(){
		tts.speak(BlindUserSpeechToTextActivity.this, getResources().getString(R.string.blind_try_again));
		while(tts.isSpeakingNow());
		BlindUserSpeechToTextActivity.this.runOnUiThread(new Runnable() {
			public void run() {
				BlindUserSpeechToTextActivity.this.launchRecognizer();
			}
		});
	}

	public void multitouchMotionEvent(int numberOfFingers, GestureType gestureType) {
		if(ttsReady){
			tts.stopSpeaking();
			switch(gestureType){
			case DOWN:
				break;
			case UP:
				break;
			case LEFT:
				Intent resultIntent = new Intent();
				resultIntent.putExtra("result", result);
				resultIntent.putExtra("item_to_get", itemToGet);
				BlindUserSpeechToTextActivity.this.setResult(RESULT_OK, resultIntent);
				BlindUserSpeechToTextActivity.this.finish();
				break;
			case RIGHT:
				new Thread(new Runnable() {
					public void run() {
						relaunchTTS();
					}
				}).start();
				break;
			}
		}
	}

	private void launchRecognizer(){
		Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
		intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
		intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);
		startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == VOICE_RECOGNITION_REQUEST_CODE)
			if(resultCode == RESULT_OK) {
				ArrayList<String> textMatchList = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
				if(!textMatchList.isEmpty()){
					result = textMatchList.get(0);
					if(itemToGet.equals("user name")){
						result = result.replace(" at ", "@");
						result = result.toLowerCase();
					}
					tts.speak(BlindUserSpeechToTextActivity.this, "You said ... " + result +"... Swipe left to go back to the previous menu with this result. Swipe right to try again.");
					if(itemToGet.equals("user name"))
						result = result.replace(" ", "");
				}
				else
					tts.speak(BlindUserSpeechToTextActivity.this, getResources().getString(R.string.blind_speech_not_recognized));
			}
			else if(resultCode == RecognizerIntent.RESULT_NO_MATCH){
				tts.speak(BlindUserSpeechToTextActivity.this, getResources().getString(R.string.blind_speech_not_recognized));
			}
		super.onActivityResult(requestCode, resultCode, data);
	}
}
